package com.example.demo.config;

import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.util.HashMap;

/**
 * 统一数据格式处理
 */
@ControllerAdvice
public class ResponseAdvice implements ResponseBodyAdvice {
    //以下两个方法都要实现,第一个返回true,代表此方法不走,走下面的.

    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return true;//返回true,代表此方法不走,走下面的beforeBodyWrite.
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        HashMap<String,Object> result=new HashMap<>();
        result.put("code","-1");
        result.put("msg","");
        result.put("data",body);
        return result;

    }
}
